package com.adgox.tiantianbiting.home.category;

import android.util.Log;

import com.adgox.tiantianbiting.bean.BaseResponseBean;
import com.adgox.tiantianbiting.bean.PageBean;
import com.adgox.tiantianbiting.bean.SubProgramBean;
import com.adgox.tiantianbiting.bean.TopBannerBean;
import com.adgox.tiantianbiting.bean.UserInfo;
import com.adgox.tiantianbiting.net.callback.JsonCallback;
import com.adgox.tiantianbiting.utils.ApiConstant;
import com.adgox.tiantianbiting.utils.DialogUtils;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.GetRequest;
import com.rae.swift.session.SessionManager;

import java.util.List;

import static android.content.ContentValues.TAG;

/**
 * Created by tang on 2018/12/26.
 */

public class CategoryPresenterImpl implements CategoryContract.Presenter {

    private CategoryContract.View mView;
    private int userId = SessionManager.getDefault().getUser() == null?0:((UserInfo)SessionManager.getDefault().getUser()).getId();

    public CategoryPresenterImpl(CategoryContract.View mView) {
        this.mView = mView;
        mView.setPresenter(this);
    }

    @Override
    public void start() {
    }


    @Override
    public void getCategory(int categoryId, int startPage, int maxPage) {
        GetRequest<BaseResponseBean<PageBean<SubProgramBean>>> baseRequest = null;
        if (userId <= 0){
            baseRequest = OkGo.<BaseResponseBean<PageBean<SubProgramBean>>>get(ApiConstant.BASE_URL + ApiConstant.GET_PROGRAM_BY_CATEGOTY + categoryId);
        } else {
            baseRequest = OkGo.<BaseResponseBean<PageBean<SubProgramBean>>>get(ApiConstant.BASE_URL + ApiConstant.GET_PROGRAM_BY_CATEGOTY + categoryId)
                    .params("userId", this.userId);
        }
        baseRequest.params("startPoint",startPage)
                .params("maxPage",maxPage)
                .execute(new JsonCallback<BaseResponseBean<PageBean<SubProgramBean>>>() {
                    @Override
                    public void onSuccess(Response<BaseResponseBean<PageBean<SubProgramBean>>> response) {
                        DialogUtils.dismiss();
                        if (!"0".equals(response.body().getCode())){
                            mView.onGetDataFailed(response.body().getMessage());
                            return;
                        }
                        PageBean<SubProgramBean> pageBean = response.body().getData();
                        mView.onSetData(pageBean);
                    }

                    @Override
                    public void onError(Response<BaseResponseBean<PageBean<SubProgramBean>>> response) {
                        super.onError(response);
                        DialogUtils.dismiss();
                    }
                });
    }

    @Override
    public void getBanner(int categoryId) {
        //这里借用一些首页banner的bean，反正该有的都有，就复用了。
        OkGo.<BaseResponseBean<List<TopBannerBean>>>get(ApiConstant.BASE_URL+ApiConstant.GET_CATEGORY_BANNER)
                .params("categoryId",categoryId)
                .params("type",1)
                .execute(new JsonCallback<BaseResponseBean<List<TopBannerBean>>>() {
                    @Override
                    public void onSuccess(Response<BaseResponseBean<List<TopBannerBean>>> response) {
                        if (!"0".equals(response.body().getCode())){
                            mView.onGetBannerFailed(response.body().getMessage());
                            return;
                        }
                        List<TopBannerBean> cateBannerList = response.body().getData();
                        mView.onSetBanner(cateBannerList);
                    }
                });
    }

    @Override
    public void addHistory(String s) {
        OkGo.<BaseResponseBean<String>>post(ApiConstant.BASE_URL + ApiConstant.ADD_COLLECT_AND_HISTORY)
                .upJson(s)
                .execute(new JsonCallback<BaseResponseBean<String>>() {
                    @Override
                    public void onSuccess(Response<BaseResponseBean<String>> response) {

                    }
                });
    }
}
